package cn.csust.net2.manager.client.panel;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.shared.po.Graduate;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.BeanService;
import cn.csust.net2.manager.shared.service.BeanServiceAsync;
import cn.csust.net2.manager.shared.util.Factory;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class GraduateRegisterPanel extends ContentPanel {
	private FormData formData;
	private FormPanel formPanel;
	private TextField<String> workUnitTextField;
	private TextField<String> workUnitAddressTextField;
	private NumberField workUnitTelephone;
	private DateField leaveDate;
	private Button saveButton;
	private Button cancelButton;
	
	@SuppressWarnings("unused")
	private Student student = (Student)App.getUser();
	@SuppressWarnings("unused")
	private boolean editable = true;

	public GraduateRegisterPanel() {
		setHeaderVisible(false);
	}
	
	public GraduateRegisterPanel(Student student, boolean editable) {
		setHeaderVisible(false);
		this.student = student;
		this.editable = editable;
	}

	@Override
	protected void onRender(Element parent, int pos) {
		// TODO Auto-generated method stub
		super.onRender(parent, pos);
		setLayout(new CenterLayout());
		formData = new FormData("-20");
		createForm();
	}

	public void createForm() {
		formPanel = new FormPanel();
		formPanel.setHeading("请输入你的离校信息...");
		formPanel.setFrame(true);
		formPanel.setLabelWidth(80);
		formPanel.setWidth(500);

		// 工作单位
		workUnitTextField = new TextField<String>();
		workUnitTextField.setFieldLabel("工作单位");
		formPanel.add(workUnitTextField, formData);
		
		//工作单位地址
		workUnitAddressTextField = new TextField<String>();
		workUnitAddressTextField.setFieldLabel("工作单位地址");
		formPanel.add(workUnitAddressTextField, formData);
		
		//工作单位电话
		workUnitTelephone = new NumberField();
		workUnitTelephone.setFieldLabel("工作单位电话");
		formPanel.add(workUnitTelephone, formData);
		
		//离校时间
		leaveDate = Factory.createDateFieldWithFormated();
		leaveDate.setFieldLabel("离校时间");
		formPanel.add(leaveDate, formData);
		
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);  
		
		saveButton = new Button("提交");
		formPanel.addButton(saveButton);
		saveButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				submit();
			}
		});
		
		cancelButton = new Button("重置");
		formPanel.addButton(cancelButton);
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				formPanel.reset();
			}
		});
		this.add(formPanel);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void submit() {
		Graduate graduate = new Graduate();
		graduate.setUser(App.getUser());
		graduate.setWorkUnit(workUnitTextField.getRawValue());
		graduate.setWorkUnitAddress(workUnitAddressTextField.getRawValue());
		graduate.setWorkUnitTelephone(workUnitTelephone.getRawValue());
		graduate.setLeaveDate(leaveDate.getDatePicker().getValue());
		BeanServiceAsync service = GWT.create(BeanService.class);
		service.save("Graduate", graduate, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Info.display("提示", "注册成功！！！");
			}
		});
	}

}
